package pl.edu.pw.ee.buildcity.services;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import pl.edu.pw.ee.buildcity.dao.GenericDao;
import pl.edu.pw.ee.buildcity.entities.BuildDefinition;

import javax.annotation.Resource;
import java.net.MalformedURLException;
import java.util.List;

import static org.fest.assertions.Assertions.assertThat;

/**
 * @author mgrzechocinski
 * @since 2009-10-18
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:domain-context.xml", "classpath:core-context.xml", "classpath:test-context.xml"})
public class BuildDefinitionServiceIntegrationTestCase {

	@Resource(name = "buildDefinitionService")
	private BuildDefinitionService buildDefinitionService;

	@Resource(name = "jpaGenericDao")
	private GenericDao<BuildDefinition, String> dao;

	@Test
	@Transactional
	public void shouldStoreBuildDefinition() throws MalformedURLException {
		//Given
		BuildDefinition bd = new BuildDefinition().withName("myJobDefinition3");
		bd.setWorkingCopy(System.getProperty("user.dir"));
		bd.setRepositoryURL("http://example.com/trunk3");
		dao.persist(bd);

		//When
		BuildDefinition found = buildDefinitionService.getByName("myJobDefinition3");

		//Then
		assertThat(found).isNotNull();
		assertThat(found.getName()).isEqualTo("myJobDefinition3");
		assertThat(found.getWorkingCopy()).isEqualTo(System.getProperty("user.dir"));
		assertThat(found.getRepositoryURL()).isEqualTo("http://example.com/trunk3");
	}

	@Test
	@Transactional
	public void shouldReturnNullWhenBuildDefinitionNotFound() {
		//Given

		//When
		BuildDefinition found = buildDefinitionService.getByName("myJobDefinition2");

		//Then
		assertThat(found).isNull();
	}

	@Test
	@Transactional
	public void shouldUpdateEntity() throws MalformedURLException {
		//Given
		BuildDefinition bd = new BuildDefinition().withName("myJobDefinition3");
		bd.setWorkingCopy(System.getProperty("user.dir"));
		bd.setRepositoryURL("http://example.com/trunk1");
		dao.persist(bd);

		bd.setName("myEditedJobDefinition");

		//When
		BuildDefinition updated = buildDefinitionService.update(bd);

		//Then
		assertThat(buildDefinitionService.getAll(BuildDefinition.class)).hasSize(1);
		assertThat(updated.getName()).isEqualTo("myEditedJobDefinition");
	}

	@Test
	@Transactional
	public void shouldFindAllBuildDefinitions() throws MalformedURLException {
		//Given
		BuildDefinition bd1 = new BuildDefinition().withName("myJobDefinition1");
		bd1.setWorkingCopy(System.getProperty("user.dir"));
		bd1.setRepositoryURL("http://example.com/trunk1");
		dao.persist(bd1);

		BuildDefinition bd2 = new BuildDefinition().withName("myJobDefinition2");
		bd2.setWorkingCopy(System.getProperty("user.dir"));
		bd2.setRepositoryURL("http://example.com/trunk2");
		dao.persist(bd2);

		//When
		List<BuildDefinition> allBuildDefinitions = buildDefinitionService.getAll(BuildDefinition.class);

		//Then
		assertThat(allBuildDefinitions).isNotNull().hasSize(2).containsExactly(bd1, bd2);
	}
}
